//index.js
//获取应用实例
const app = getApp();
const Util = require('../../utils/util.js');

Page({
  data: {
    token:'',
    addPhoto:true,
    focus:false,
    cateId:'',
    photoName:'测试',
    isUserList:false,
    isRole:false,
    transfer:0
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function (e) {
    console.info(e);
    var that = this;
    that.setData({
      photoName: e.cateName,
      cateId: e.cateId
    });
    wx.getStorage({
      key: 'token',
      success: function (res) {
        that.setData({
          token: res.data
        });
        that.checkUserList();
      }
    });
  },
  onShow:function(e){
    var that = this;
    var aShow = app.aData.show;
    if (aShow) {
      wx.getStorage({
        key: 'token',
        success: function (res) {
          that.setData({
            token: res.data
          });
          that.checkUserList();
        }
      });
    }
  },
  checkUserList:function(){
    var that = this;
    var cate_id = that.data.cateId;
    var token = that.data.token;
    Util.getUserShow({
      "token": token,
      "cate_id": cate_id
    }, function (re) {
      if (re.code == 200) {
        if(re.data==1){
          var isUserList = true;
        }else{
          var isUserList = false;
        }
        that.setData({
          isUserList: isUserList,
          isRole: re.isRole,
          transfer : re.transfer
        });
      }
      else {
        Util.showMsgDialog(re.msg);
      }
    }, function (err) {
      Util.showMsgDialog("操作失败");
    });
  },
  moreFunc:function(){
    wx.navigateTo({
      url: 'test?id=1'
    })
  },
  photoCode:function(){
    var that = this;
    var cate_id = that.data.cateId;
    var photoName = that.data.photoName;
    wx.navigateTo({
      url: 'two_code?cate_id=' + cate_id + '&name=' + photoName
    })
  },
  canAddPhotos:function(){
    var that = this;
    var cat_id = that.data.cateId;
    wx.navigateTo({
      url: 'role?cate_id=' + cat_id
    })
  },
  setPhoneName:function(){
    var that = this;
    var cat_id = that.data.cateId;
    var token = that.data.token;
    setTimeout(function () {
      var content = that.data.inputname;
      if (!content) {
        Util.showMsgDialog("请输入相册名称");
        isdoing = false;
      }
      that.hidePhoto();
      Util.setCateName({
        "name": content,
        "cat_id":cat_id,
        "token": token
      }, function (re) {
        if (re.code == 200) {
          that.setData({
            photoName: content
          });
        }
        else {
          Util.showMsgDialog(re.msg);
        }
      }, function (err) {
        Util.showMsgDialog("操作失败");
      });
    }, 300);
  },
  transferRole:function(){
    var that = this;
    var cat_id = that.data.cateId;
    wx.navigateTo({
      url: 'transfer?cate_id=' + cat_id
    })
  },
  addPhoto: function () {
    var that = this;
    that.setData({
      addPhoto: false,
      focus: true
    })
  },
  showPhoto: function (e) {
    this.setData({
      addPhoto: false,
      focus: true
    })
  },
  hidePhoto: function (e) {
    this.setData({
      addPhoto: true,
      focus: false
    })
  },
  bingData: function (e) {
    this.setData({
      inputname: e.detail.value
    });
  },
  settingImages:function(){
    var that = this;
    var cateId = that.data.cateId;
    wx.navigateTo({
      url: 'manage?cateId='+cateId
    })
  },
  isUserList:function(e){
    var that = this;
    var cate_id = that.data.cateId;
    var token = that.data.token;
    if (e.detail.value){
      var is_user_list = 1;
    }else{
      var is_user_list = 0;
    }
    Util.setUserShow({
      "is_user_list": is_user_list,
      "token": token,
      "cate_id": cate_id
    }, function (re) {
      if (re.code == 200) {
        
      }
      else {
        Util.showMsgDialog(re.msg);
      }
    }, function (err) {
      Util.showMsgDialog("操作失败");
    });
  },
  scrolltoupper :function(){
    wx.startPullDownRefresh();
  },
  upper: function (e) {
    console.log(e)
  },
  lower: function (e) {
    console.log(e)
  },
  scroll: function (e) {
    console.log(e)
  },
  onPullDownRefresh:function(){
    wx.startPullDownRefresh();
    setTimeout(function () {
      wx.stopPullDownRefresh();
    }, 300);
  }
})
